package com.itcpeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcpeng.dao.OrderSettingDao;
import com.itcpeng.pojo.OrderSetting;
import com.itcpeng.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = OrderSettingService.class)
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;


    /**
     * 预约设置服务
     *
     * @param list
     */
    @Transactional
    @Override
    public void doImport(List<OrderSetting> list) {
        //遍历list
        for (OrderSetting orderSetting : list) {
            //调用dao按日期查询看是否存在
            OrderSetting idb = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
            if (idb != null) {
                //如果存在则更新数据
                idb.setNumber(orderSetting.getNumber());
                orderSettingDao.updateNumber(idb);
            } else {
                //不存在则插入
                orderSettingDao.add(orderSetting);

            }


        }
    }

    //根据日期查询预约设置数据
    @Override
    public List<OrderSetting> ordersettingMonth(String month) {
        //按时间范围查询
        String startDate = month + "-01";
        String endDate = month + "-31";
        List<OrderSetting> list = orderSettingDao.findOrderSettingDate(startDate, endDate);
        return list;
    }

    //根据日期修改可预约的人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        OrderSetting date = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
        if (date != null) {
            //当前日期已经进行了预约设置,需要进行修改操作
            date.setNumber(orderSetting.getNumber());
            orderSettingDao.updateNumber(date);
        } else {
            //当前么有日期预约操作,进行添加
            orderSettingDao.add(orderSetting);
        }
    }
    @Override
    public void deleteOrderSettingByDate(Date date) {
        orderSettingDao.deleteOrderSettingByDate(date);
    }
}
